var express = require('express');
var router = express.Router();
var User = require('../models/user');
var Post = require('../models/post');

/* GET users listing. */
router.get('/:user', function (req, res, next) {
    var user = new User(req.params.user);
    user.get(function (err, user) {
        if (!user) {
            req.flash('message', '用户不存在');
            res.redirect('/');
        }

        var post = new Post(user.name);
        post.get(function (err, posts) {
            if (err) {
                req.flash('message', err);
                res.redirect('/');
            }
            res.redirect('user', {title: user.name, posts: posts});
        });
    });
});

module.exports = router;
